package com.bp.ice.service.impl;

import com.bp.ice.common.config.PageProperties;
import com.bp.ice.common.exception.ServiceException;
import com.bp.ice.common.vo.JsonResult;
import com.bp.ice.common.vo.PageObject;
import com.bp.ice.dao.SysUserDao;
import com.bp.ice.entity.SysUser;
import com.bp.ice.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工信息呈现
 * @author Administrator
 *
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private PageProperties pageProperties;

    //修改状态
    @Override
    public int validById(Integer id, Integer valid) {
        //1.参数校验
        if(id==null||id<1)
            throw new IllegalArgumentException("无效参数异常");
        if(valid!=1&&valid!=0)
            throw new IllegalArgumentException("状态值不正确");
        //2.更新状态
        int rows=sysUserDao.validById(id, valid);
        //3.返回结果
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        return rows;
    }
    //删除用户信息
    @Override
    public int deleteObjects(Integer... ids) {
        //参加校验
        //System.out.println("ids"+ids);
        if (ids==null||ids.length==0)
            throw new IllegalArgumentException("请选择");
        //执行操作
        int rows = sysUserDao.deleteObjects(ids);
        //判断并返回
        if(rows==0)
            throw new ServiceException("记录已经不存在");
        return rows;
    }

    @Override
    public JsonResult findPageObjects(String username, Integer pageCurrent) {
        //1.参数校验
        if (pageCurrent == null || pageCurrent < 1)
            throw new IllegalArgumentException("页码值不正确");
        //2.查询总记录数
        int rowCount = sysUserDao.getRowCount(username);
//        if (rowCount == 0)
//            throw new ServiceException("记录已经不存在");
        //3.查询当前页面记录
        int pageSize = pageProperties.getPageSize();//功能正常用，idea的问题
        //System.out.println("pageSize====" + pageSize);
        int startIndex = (pageCurrent - 1) * pageSize;
        List<SysUser> records = sysUserDao.findPageObjects(username, startIndex, pageSize);
        //4.封装查询结果并返回
        JsonResult jsonResult = new JsonResult(new PageObject<>(records, rowCount, pageCurrent, pageSize));
        if (rowCount==0){
            jsonResult.setState(2);
            jsonResult.setMessage("记录为空");
        }
        return jsonResult;
    }
}
